/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.validating;

import com.andersen.web.beans.RegisterCommandForm;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

/**
 *
 * @author Corvus 11
 */
@Component
public class RegisterValidator extends AbstractValidator{

    @Override
    public boolean supports(Class type) {
        return RegisterCommandForm.class.isAssignableFrom(type);
    }

    @Override
    public void validate(Object obj, Errors errors) {
        
        
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login",
			"required.login", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
			"required.password", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
			"required.confirmPassword", "Field name is required.");
        
        RegisterCommandForm fmt = (RegisterCommandForm)obj;
        
        if (fmt.getLogin() != null && (!fmt.getLogin().isEmpty()) && (!userService.isLoginFree(fmt.getLogin()))) {
            errors.rejectValue("login", "alreadyusing.login");
        }
        
        if (fmt.getPassword()!=null && fmt.getConfirmPassword()!=null && !fmt.getPassword().equals(fmt.getConfirmPassword())) {
            errors.rejectValue("password", "notmatch.password");
        }
    }
    
}

/*
 * Кусок для регистрации сотрудника
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
			"required.userName", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname",
			"required.surname", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "patronymic",
			"required.patronymic", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "skype",
			"required.skype", "Field name is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone",
			"required.phone", "Field name is required.");
        
        RegisterCommandForm fmt = (RegisterCommandForm)obj;
        
        
        
        if ("NONE".equals(fmt.getOfficeId())) {
            errors.rejectValue("officeId","required.officeId");
        }
        if (fmt.getSkype() != null && (!fmt.getSkype().isEmpty()) && fmt.getSkype().matches("^.*[^a-zA-Z0-9.].*$")) {
            errors.rejectValue("skype", "incorrect.skype");
        } else {
            if (!employeeService.isSkypeFree(fmt.getSkype())) {
                errors.rejectValue("skype", "alreadyusing.skype");
            }
        }
        String phn = fmt.getPhone();
        if (phn != null && !phn.isEmpty()) {
            if (phn.charAt(0) == '+') phn = phn.substring(1);
            if (phn.matches("^.*[^0-9].*$")) {
                errors.rejectValue("phone", "incorrect.phone");
            } else {
                if (!employeeService.isPhoneFree(fmt.getPhone())) {
                errors.rejectValue("phone", "alreadyusing.phone");
            }
            }
        }*/